﻿/**
* @file pid.c
* @brief this is a brief description
* @details this is the detail description.
* @author fulong 
* @data 2015/1/10
* @version 1.0
* @par Copyright (c):
* @par First build
*     version: fulong 
*     data: 2015/1/10
*/
#include "pid.h"

#ifdef PID_ENABLE
PID_struct pid;
int32 cur_value;

void pid_init(void)
{
    pid.derivative = 0;
    pid.uk = 0;
    pid.pid_max = 0;
    pid.pid_min = 0;
    pid.integral = 0;
    pid.lasterror = 0;
    pid.preverror = 0;
    pid.proportion = 0;
    pid.setpoint = 0;
    pid.get_error_value = (int32(*)())0;
}
double inc_pid_process(uint8 *proc_err,double feedback)
{
    /*!< e(k) */
    double err0;
    double calpid;
    if (pid.get_error_value == (int32(*)())0)
    {
        *proc_err = 0xFF;
    } else
    {
        err0 = pid.setpoint - feedback;
        calpid = pid.proportion *(err0 - pid.lasterror) + err0 * pid.integral + pid.derivative * (err0 - 2*pid.lasterror +pid.preverror);
        pid.preverror = pid.lasterror;
        pid.lasterror = err0;
    }
    return (calpid);
}
#endif
